// pages/duBanJiaoliu/duBanJiaoliu.js
import WxValidate from '../../utils/WxValidate.js';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: app.globalData.url,
	  joinedList:[ ],
    discussList:[],
	  showmask: false,//是否显示弹框  true：显示   false：隐藏 
    count:0,
    showDiscuss: false,//评论回复弹框
    toUserId:'',
  },
  //发消息按钮（显示弹框)
	msg_showAlert:function(e){
    console.log(1);
		this.setData({
			showmask: true
		})
	},
  //隐藏弹框
	hideMask:function(e){
		this.setData({
			showmask:false
		})
	},

  //发送消息
  sendMsg: function (e) {
    var _this = this;
    var params = e.detail.value
    console.log(params);
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
      return false
    }
    wx.request({
      url: app.globalData.url + '/discuss/addDiscuss',
      header: app.header(),
      method: "POST",
      data: { content: e.detail.value.content, permissionId: _this.data.id },
      success: function (res) {
        console.log(res);
        if (res.data.status=='error') {
          wx.showToast({
            title: res.data.data.content,
            icon: 'loading',
            duration: 1500
          })
        } else {
          wx.showToast({
            title: '保存成功',//这里打印出登录成功
            icon: 'success',
            duration: 1000,
            success: function () {
              console.log(_this.data.id);
              setTimeout(function () {
                //要延时执行的代码
                _this.setData({
                  showmask: false
                })
                _this.loadData(_this.data.id);
              }, 2000) //延迟时间
            }
          })
        }
      }
    })
  },

  //显示回复弹框
  showDiscuss(e) {
    var _this =this;
    var index = e.currentTarget.dataset.index;   
    var toUserId = _this.data.discussList[index].userId;
    this.setData({
      showDiscuss: true,
      toUserId: toUserId
    })
  },
  //关闭回复弹框
  hideDiscuss() {
    this.setData({
      showDiscuss: false,
      toUserId: '',
    })
  },

  sendDiscuss:function(e) {//发送回复
    var _this = this;

    if (!e.detail.value.contentDis) {
      this.showModal({ msg: '内容不能为空' })
      return false
    };
    var toUserId = _this.data.toUserId;
    wx.request({
      url: app.globalData.url + '/discuss/addDiscuss',
      header: app.header(),
      method: "POST",
      data: { content: e.detail.value.contentDis, permissionId: _this.data.id, toUserId: toUserId},
      success: function (res) {
        console.log(res);
        if (res.data.status == 'error') {
          wx.showToast({
            title: res.data.data.content,
            icon: 'loading',
            duration: 1500
          })
        } else {
          wx.showToast({
            title: '保存成功',//这里打印出登录成功
            icon: 'success',
            duration: 1000,
            success: function () {
              console.log(_this.data.id);
              setTimeout(function () {
                //要延时执行的代码
                _this.setData({
                  showDiscuss: false
                })
                _this.onLoad();
              }, 2000) //延迟时间
            }
          })
        }
      }
    })
  },


  //报错 
  showModal(error) {
    wx.showModal({
      content: error.msg,
      showCancel: false,
    })
  },
  //验证函数
  initValidate() {
    const rules = {
      content: {
        required: true,
        // minlength: 2
      }

    }
    const messages = {
      content: {
        required: '内容不能为空'
      }
    }
    this.WxValidate = new WxValidate(rules, messages);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     var _this =this;
    _this.initValidate();
    //  var id = options.id;
     var id=36;
    _this.loadData(id);
  },

 loadData:function(id){
   var _this = this;
   wx.request({
     url: app.globalData.url + '/discuss/showDiscuss/' + id,//新增会议接口
     header: app.header(),
     method: 'GET',
     success: function (res) {
       if (res.data.status == 'success') {
         if (res.data.result.supervisionDiscussEOS != null) {
           var count = res.data.result.supervisionDiscussEOS.length;
         }

         _this.setData({
           id: id,
           joinedList: res.data.result.supervisionUserList,
           discussList: res.data.result.supervisionDiscussEOS,
           count: count,
         })
       }
     }
   })
 },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})